<template>
    <div id="organization">
        <!-- 对部门名称进行查询 -->
       <el-form :model="orFrom"  :rules="rulesname"  ref="nameForm"> 
            <el-form-item prop="orname">
                <el-input v-model="orFrom.orname" style="width:300px" clearable   prefix-icon="el-icon-search" placeholder="请输入部门名称"></el-input>
                <el-button plain style="margin-left:1%" @click="Addbm('nameForm')" type="primary"  icon="el-icon-plus">添加</el-button> 
            </el-form-item>
       </el-form>

        
         <!-- 点击添加弹出 -->
         <el-dialog title="添加职位" :visible.sync="tjdialogFormVisible">
            <el-form  :model="addForm" :rules="rules"  ref="addForm" label-width="100px" lcass="demo-ruleForm">
                <el-form-item label="职位名称" prop="bmname" >
                <el-input v-model="addForm.bmname" placeholder="请输入名称" clearable>
                </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="tjdialogFormVisible = false">取 消</el-button>
                <el-button @click="resetForm('addForm')">重置</el-button>
                <el-button type="primary" @click="Add('addForm')">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 循环表 -->
        <el-table
            :data="processedData"
            border
            style="width: 100%"
            row-key="organizationID"
            default-expand-all
            :tree-props="{ children: 'children' }"
        >
            <el-table-column fixed prop="organizationName" label="部门名称" width="600"> </el-table-column>
            <el-table-column fixed="right" label="操作" width="300">
                <template slot-scope="scope">
                        <el-button plain size="small" type="primary" @click="Addzw(scope.row)"><i class="el-icon-plus"></i>添加</el-button>
                </template> 
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    name:'organization',
    data(){
        return{
            orFrom:{
                orname:'',
            },
            tjdialogFormVisible:false,
            addForm:{
                bmname:'',
            },
            rules: {
                bmname: [
                    { required: true, message: '请输入部门', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                ParentId: [{ required: true, message: '请选择部门', trigger: 'change' }]
            },
            rulesname:{
                orname: [
                        { required: true, message: '请输入组织', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ],
            },
            processedData:[],
            ff:0,
            list:[]
        }
    },
    created(){
       this.show();
       this.jy();
    },
    methods:{
        jy(){
             axios.get("https://localhost:44328/OA_Oraganization/GetGrade")
            .then((result)=>{
              for (let index = 0; index <  result.data.length; index++) {
                 //console.log(result.data[index].organizationID)
                this.list.push(result.data[index].organizationID)
              }
            
            })
        },
         Addzw(row){
              this.ff=row.organizationID
              
              
              console.log(this.list.includes(row.organizationID))
              //console.log(row.organizationID)
             if(row.organizationID==1){
                  this.$message.error('点击部门列添加职位哦！');
                 this.tjdialogFormVisible=false;
             }
             else if(this.list.includes(row.organizationID)){
                  this.$message.error('不能点击自己添加职位哦！');
                 this.tjdialogFormVisible=false;
             }
             else{
                // console.log(row.organizationID)
                this.tjdialogFormVisible=true;
             }
           
        },
        show(){
            axios.get("https://localhost:44328/OA_Oraganization/GetOrganizations")
            .then((result)=>{
                this.processedData=result.data
                var treeData = this.convertToTreeData(this.processedData, 0);
                this.processedData= treeData;
                console.log( this.processedData)
            })
        },
         convertToTreeData(data,pid) {
            const result = [];
            let temp = [];
            for (let i = 0; i < data.length; i++) {
                if (data[i].parentId===pid) {
                    const obj = 
                        {
                            organizationName: data[i].organizationName,
                            organizationID: data[i].organizationID,
                            parentId: data[i].parentId,
                            grade: data[i].grade,
                            update_date: data[i].update_date,
                            children:[]
                        };
                    temp = this.convertToTreeData(data, data[i].organizationID);
                    if (temp.length > 0) {
                        obj.children = temp;
                     }
                    result.push(obj);
                }
            }
            return result;
        },
        Addbm(formName){
           this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('https://localhost:44328/OA_Oraganization/AddPosition?OrganizationName='+this.orFrom.orname+'')
            .then((result)=>{
                if(result.data==1){
                    this.$message({
                        message: '添加部门成功',
                        type: 'success'
                    });
                    this.tjdialogFormVisible=false;
                     this.show();
                }else {
                    this.$message.error('添加失败');
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        },
        Add(formName){
            console.log(this.ff)
           this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
            axios.post('https://localhost:44328/OA_Oraganization/AddDepartment?ParentId='+this.ff+'&OrganizationName='
            +this.addForm.bmname+'')
            .then((result)=>{
                if(result.data==1){
                    this.$message({
                        message: '添加组织成功',
                        type: 'success'
                    });
                     this.show();
                }else {
                    this.$message.error('添加失败');
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
};
</script>
<style scoped>
</style>